var customFormControls = require('./zy-form-item-behavior');
var { getObj } = require('../../common/collection');

Component({
    behaviors: [customFormControls],
    options: {
        dynamicSlots: true, // 启用动态 slot
    },
    properties: {
        ele_type: {
            type: String,
            value: ''
        },
        name: String,
        widget_config: null
    },
    data: {
        /**
         * 
         * @param {string} path 
         */
        getConfig(path = '') {
            // console.log(this.name, this.widget_config);
            if (!this.widget_config) {
                return null
            }
            return getObj(this.widget_config, path)
        }
    },
    methods: {
        m_formitem_change(e) {
            console.log('m_formitem_change', e);
            this.form.setModelByName(this.data.name, e.detail.value)
        },
        onInitModel(newVal, handle) {
            // console.log('onInitModel', newVal);
            // console.log(this.curInputComCtx);
            let self = this
            function defHandle() {
                if (self.data.ele_type  === 'radio-group') {
                    self.curInputComCtx?.setChecked(newVal);
                    return;
                }
                if (self.data.ele_type  === 'checkbox-group') {
                    self.curInputComCtx?.setMutlipleChecked(newVal);
                    return;
                }
                if (self.data.ele_type  === 'select') {
                    self.curInputComCtx?.setValues(newVal);
                    return;
                }
                self.curInputComCtx?.setVal(newVal)
            }
            if (handle) {
                handle(this.data.name, newVal, {
                    ctx: this,
                    defHandle
                });
                return;
            }
            else {
                defHandle()
            }
        },
        getInputCtx() {
            return  this.curInputComCtx
        }
    },
    lifetimes: {
        attached() {
            this.setCurInputCtx(this.selectComponent('.input'))
            // console.log(this.dataset);
            this.ownerComonent = this.selectOwnerComponent();
            this.form = this.ownerComonent.selectComponent('#' + this.dataset.form)
            // console.log( this.form);
            this.form.setFormItem(this.data.name, this)
        }
    }
})

